package com.qilu.pe.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import androidx.appcompat.widget.AppCompatEditText;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 意见反馈
public class FeedBackActivity extends Base2Activity {
    private AppCompatEditText et_content;
    private TextView tv_ok;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_feed_back;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        et_content = findViewById(R.id.et_content);
        tv_ok = findViewById(R.id.tv_ok);

        setHeadTitle("意见反馈");

        setOnClickListener(tv_ok);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_ok) {
            String content = et_content.getText().toString().trim();
            if (TextUtils.isEmpty(content)) {
                ToastUtils.showShort("请输入您的意见反馈");
                return;
            }

            hideProgress();
            reqFeedback(content);
            showProgress();
        }
    }

    private Disposable reqFeedback(String content) {
        return APIRetrofit.getDefault().reqFeedback(Global.HEADER, content)
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 baseResult2) throws Exception {
                        hideProgress();
                        if (baseResult2.isSuccess()) {
                            ToastUtils.showShort(baseResult2.getMsg());
                            finish();
                        } else {
                            ToastUtils.showShort(baseResult2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("提交失败.请重试");
                    }
                });
    }
}